package com.woniu.woniu_bx.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniu.woniu_bx.exception.ManagerExistsException;
import com.woniu.woniu_bx.mapper.ManagerMapper;
import com.woniu.woniu_bx.pojo.Manager;
import com.woniu.woniu_bx.pojo.PageBean;
import com.woniu.woniu_bx.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.sql.Wrapper;
import java.util.List;

/**
 * @Description 管理员
 * @Author zhangjin
 * @Date 2022/8/27 16:21
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {
    @Autowired
    private ManagerMapper managerMapper;

    @Override
    public void addManager(Manager manager) {
        if (managerMapper.getManagerNumByAccount(manager.getAccount()) == 0) {
            manager.setStatus("y");
            //不存在账户则添加
            managerMapper.insert(manager);
        } else {
            throw new ManagerExistsException("管理员账号已存在");
        }
    }

    @Override
    public void logicDel(Integer id) {
        //逻辑删除
        managerMapper.logicDel(id);
    }

    @Override
    public Manager getManagerByAccount(String account) {
        //todo 根据账号查询管理员，密码交给springsecurity处理
        Manager manager = managerMapper.getManagerByAccountPwd(account);// 根据账号查询管理员
        return manager;
    }

    @Override
    public void updateManager(Manager manager) {
        // 修改管理员账号信息，mybatis-plus内置updateById，可实现动态修改
        managerMapper.updateById(manager);
    }



    /**
     * @Description 获取分页的订单列表
     * @Date 15:25 2022/8/29
     * @Author 魏才林
     * @Param [pageNum, pageSize] 页码 , 分页数量
     * @return
     **/
    @Override
    public PageBean<Manager> getAllManagers(Integer pageNum, Integer pageSize) {

        Page page = PageHelper.startPage(pageNum, pageSize);
        List<Manager> managers = managerMapper.getAllManagers();

        PageBean<Manager> managerPageBean = new PageBean<>();
        managerPageBean.setPageSize(pageSize);
        managerPageBean.setData(managers);
        managerPageBean.setCurrPage(pageNum);
        managerPageBean.setCurrPageSize(managers.size());
        managerPageBean.setTotalPages(page.getPages());
        managerPageBean.setTotalNums((int) page.getTotal());

        return managerPageBean;
    }




}
